﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class trip_plan_plan : System.Web.UI.Page
{

    ctpDataContext db;
    int no=0;

    protected void Page_Load(object sender, EventArgs e)
    {
        db = new ctpDataContext();
        if(Request["no"]!=null) no=int.Parse(Request["no"]);


        if (!this.IsPostBack)
        {
            var typedata = from p in db.ct_s_types
                           orderby no descending
                           select p;

            this.DropDownList1.DataSource = typedata;
            this.DropDownList1.DataTextField = "title";
            this.DropDownList1.DataValueField = "no";

            var data=from p in db.ct_s_profiles
                     where p.valid==true&&p.ct_no==no
                     orderby p.ranking descending
                     select p;

            string dataitemstring = "";

            foreach(ct_s_profile c in data.Take(30))
            {
                if(c.main_pic_fn==null)
                    c.main_pic_fn="coming.jpg";
                else
                    c.main_pic_fn=c.main_pic_fn.Trim();

                if (dataitemstring == "")
                    dataitemstring = c.no.ToString();
                else
                    dataitemstring += "," + c.no.ToString();


            }

            this.DataList1.DataSource=data.Take(30);
            this.DataBind();

            string myjs = "dataitemstring='" + dataitemstring + "';";
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "myjs1", myjs, true);


            this.DropDownList1.Items.Add(new ListItem("Most Recommended", "99"));
            this.DropDownList1.Items.FindByValue("99").Selected = true;

        }


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string myjs = "alert('"+DateTime.Now.ToString()+"')";
        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(string), "myjs1", myjs, true);
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

        //string cc = (( HyperLink )e.Item.FindControl("Hyperlink3")).Text;
        string cc = (( Label )e.Item.FindControl("Label6")).Text;
        int newno = int.Parse(((Label)e.Item.FindControl("Label6")).Text);

        ct_s_profile p1 = db.ct_s_profiles.Single(p => p.no == newno && p.valid == true);

        string r1string = "", r2string = "";

        var r1 = from p in db.ct_s_relationships
                 where p.ct_s_no1 == newno&&p.type==1
                 select p;

        var r2 = from p in db.ct_s_relationships
                 where p.ct_s_no1 == newno&&p.type==2
                 select p;

        var r3 = from p in db.ct_s_relationships
                 where p.ct_s_no2 == newno && p.type == 2
                 select p;

        foreach (ct_s_relationship r in r1)
        {
            if (r1string == "")
                r1string = r.ct_s_no2.ToString();
            else
                r1string +=","+r.ct_s_no2.ToString();

        }


        foreach (ct_s_relationship r in r2)
        {
            if (r2string == "")
                r2string = r.ct_s_no2.ToString();
            else
                r2string += "," + r.ct_s_no2.ToString();

        }

        foreach (ct_s_relationship r in r3)
        {
            if (r2string == "")
                r2string = r.ct_s_no1.ToString();
            else
                r2string += "," + r.ct_s_no1.ToString();

        }

        string fn = (( Image)e.Item.FindControl("Image1")).ImageUrl;

        string title=p1.title.Trim();
        if(title.Length>12) title=title.Substring(0,12);

        string myjs = "insert_new_node1('" + fn + "'," + newno.ToString() + ",'" + r1string + "','" + r2string + "','" + title + "');checknear();";
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "myjs1", myjs, true);
  

    }

    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string dataitemstring = "";
        
        if (this.DropDownList1.SelectedValue == "99")
        {
            var data = from p in db.ct_s_profiles
                       where p.valid == true && p.ct_no == no
                       orderby p.ranking descending
                       select p;


            foreach (ct_s_profile c in data.Take(30))
            {
                if (c.main_pic_fn == null)
                    c.main_pic_fn = "coming.jpg";
                else
                    c.main_pic_fn = c.main_pic_fn.Trim();

                if (dataitemstring == "")
                    dataitemstring = c.no.ToString();
                else
                    dataitemstring +=","+ c.no.ToString();

            }

            this.DataList1.DataSource = data.Take(30);
            this.DataBind();

            string myjs = "dataitemstring='" + dataitemstring + "';checknear();";
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "myjs1", myjs, true);


        }
        else
        {
            var data = from p in db.ct_s_profiles
                       where p.valid == true && p.ct_no == no
                       where p.type_no == int.Parse(this.DropDownList1.SelectedValue)
                       orderby p.ranking descending
                       select p;

            foreach (ct_s_profile c in data)
            {
                if (c.main_pic_fn == null)
                    c.main_pic_fn = "coming.jpg";
                else
                    c.main_pic_fn = c.main_pic_fn.Trim();

                if (dataitemstring == "")
                    dataitemstring = c.no.ToString();
                else
                    dataitemstring += "," + c.no.ToString();


            }

            this.DataList1.DataSource = data;
            this.DataBind();

            string myjs = "dataitemstring='"+dataitemstring+"';checknear();";
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "myjs1", myjs, true);

        }


    }


}
